!function (){
    var $bgimg = $('.openBgImage');
    $bgimg.hover(function(){
		var $this = $(this);
		if($this.css("backgroundImage") == "none"){
		    return false;
		}else {
		    $this.css('position','relative');
		    if($("#chooseList").get(0)){
		        $this.append($('#chooseList'));
		        $('#chooseList').show();
		    }else {
		        $this.append('<div id="chooseList"><a class="imgLink">打开</a><a class="imgDown" href="" download="backgroundImage">下载</a></div>');
		        $this.find(".imgLink").on('click',openAndDownImg);
		        $('body').append('<div class="openBgImageMk"></div><div id="bigBgBox"><img src=""><button class="btn btn-default closeBtn">关闭图片</button></div><style>#chooseList {position: absolute;left: 0;bottom: 0;background-color: #ccc;opacity: 0.6;width: 100%;height: 20px;display: none;}#chooseList a{display: inline-block;width: 50%;text-align: center;cursor: pointer;}#chooseList a:hover{background-color: #fff;}.openBgImageMk{position:fixed;top:0;right:0;left:0;bottom:0;z-index:1987;display:none;background-color:#000;opacity:0.5;}#bigBgBox {position: absolute;top: 50%;left:50%;z-index: 1990;overflow: hidden;}#bigBgBox button {display: none;cursor: pointer;position: absolute;right: 0;top: 0;z-index: 200;opacity:0.7;}</style>');
		        $this.find("#chooseList").show();
		        $('#bigBgBox').find('.closeBtn').on('click',closeImgOnClick);
		    };
		    $thisImg = $('#chooseList').parent();
		    src = $thisImg.css("backgroundImage").replace(/url\(\"([^"]+)\"\)/i, "$1").replace("'",'').replace('"',''); 
		    $('.imgDown').attr('href',src); 
		}
    },function(){
        $('#chooseList').hide();
        $("#chooseList").children().removeAttr('href'); 
    });
    var openAndDownImg = function (e) {
            e.stopPropagation();
            var $this = $(this); 
            var $bigBgBox = $('#bigBgBox');
            var $thisImg = $('#chooseList').parent();
            var imgSrc = $thisImg.css("backgroundImage").replace(/url\(\"([^"]+)\"\)/i, "$1").replace("'",'').replace('"',''); 
                $('.openBgImageMk').show();        
                $bigBgBox.find('img').attr('src',imgSrc);
                var boxWidth = $bigBgBox.find('img').css('width');
                var boxHeight = $bigBgBox.find('img').css('height');
                var marginL = -(parseInt(boxWidth) / 2);
                var marginT = -(parseInt(boxHeight) / 2);
                $bigBgBox.animate({width: boxWidth,height:boxHeight,marginLeft:marginL + 'px',marginTop: marginT + 'px'},500,function () {
                    $bigBgBox.find('.closeBtn').show();
                });
        };
        var closeImgOnClick = function () {
            $('#bigBgBox').animate({width:0,height:0,marginLeft:0,marginTop:0},500,function () {
                var $this = $(this);
            $this.find('.closeBtn').add($('.openBgImageMk')).add($('#chooseList')).hide();
            });
        };
}();
